package net.suncaper.demo.service;

import net.suncaper.demo.domain.Employee;
import net.suncaper.demo.domain.EmployeeExample;
import net.suncaper.demo.domain.SearchEmployeeRequest;
import net.suncaper.demo.mapper.EmployeeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service("DBEmployeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> listEmployee() {
        return employeeMapper.selectByExample(new EmployeeExample());
    }

    @Override
    public int deleteEmployeeByPrimaryKey(String id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Employee findEmployeeByPrimaryKey(String id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> searchEmployees(SearchEmployeeRequest request) {
        EmployeeExample ex = new EmployeeExample();
//        ex.createCriteria().andEmployeeIdEqualTo(request.getId());
//        ex.or().andLastNameLike("%" + request.getName() + "%");
        return employeeMapper.selectByExample(ex);
    }

    @Override
    public int saveEmployee(Employee employee) {
        if(StringUtils.isNotBlank(employee.getEmployeeId())) {
            return employeeMapper.updateByPrimaryKey(employee);
        } else {
            return employeeMapper.insert(employee);
        }
    }
}
